package com.lpfy.dtxc.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.ViewTreeObserver;

import com.github.barteksc.pdfviewer.PDFView;
import com.shockwave.pdfium.PdfDocument;
import com.shockwave.pdfium.PdfiumCore;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: hanGuangHui
 * @data: 2024/2/24
 * @description:
 */
public class PdfToImageUtil {

    public byte[] fileToBytes(File file) {
        ByteArrayOutputStream buffer = null;
        try {
            InputStream input_stream = new BufferedInputStream(new FileInputStream(file));
            buffer = new ByteArrayOutputStream();
            byte[] data = new byte[16384]; // 16K
            int bytes_read;
            while ((bytes_read = input_stream.read(data, 0, data.length)) != -1) {
                buffer.write(data, 0, bytes_read);
            }
            input_stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer.toByteArray();
    }

    public void getBitmap(byte[] data, Context context, PDFView pdfView, OnPdfToImageSuccess onPdfToImageSuccess) {
        //使用这种方式获取控件宽高
        final List<Bitmap> bitmapList = new ArrayList<>();
        ViewTreeObserver vto = pdfView.getViewTreeObserver();
        final int[] pdfCount = new int[1];
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Log.e("-----",pdfView.getWidth()+" === "+pdfView.getHeight());
                pdfView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                PdfiumCore pdfiumCore = new PdfiumCore(context);
                try {
                    PdfDocument pdfDocument = pdfiumCore.newDocument(data);
                    pdfCount[0] = pdfiumCore.getPageCount(pdfDocument);
                    for (int i = 0; i < pdfCount[0]; i++) {
                        //打开pdf文件 第二个参数为页码
                        pdfiumCore.openPage(pdfDocument, i);
                        //调用native方法，将Pdf页面渲染成图片
                        Bitmap bitmap = Bitmap.createBitmap(pdfView.getWidth(),
                                pdfView.getHeight(), Bitmap.Config.RGB_565);
                        //获取图片 最后一个参数 是否显示电子签章
                        pdfiumCore.renderPageBitmap(pdfDocument, bitmap, i, 0, 0,
                                pdfView.getWidth(), pdfView.getHeight(), true);
                        bitmapList.add(bitmap);
                    }
                    if(onPdfToImageSuccess!=null){
                        onPdfToImageSuccess.success(bitmapList);
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }

    public interface OnPdfToImageSuccess{
        void success(List<Bitmap> bitmaps);
    }
}
